<?php
header ( "Content-type:text/html;charset=utf-8" );
class InsertUser {

	private $dbUtil;
    function  __construct(){
   		require_once('../db/DB.class.php');
    }
    
    //查询用户
    public function query($user, $email){
    	$sql = "SELECT COUNT(*) AS total FROM userInfo t WHERE t.email='".$email."' OR t.userName= '".$user."'";
    	$dbUtil = new DB();
    	$result = $dbUtil -> queryValue($sql);
    	
    	return $result;
    }
    
    //保存用户
    public function insertUsers($user, $email, $pwd){
    	
 		$nowDate =  date('Y-m-d H:i:s',time());
    	$values = array("userName"=>$user,"email"=>$email,"passwd"=>$pwd,"addTime"=>$nowDate);
    	$dbUtil = new DB();
		try{
			$result = $dbUtil -> insert_array($values, "userInfo");
    		return $result;
		}catch( Exception $e ) {
			return "0";  
		}
    	
    }
    
    /**
     * 获取用户 
     */
    public function queryUser($userName,$pwd){
   		$dbUtil = new DB();
    	$sql = "SELECT t.email,t.id,t.userName,t.mind,t.head,p.province as provinces,c.city FROM userInfo t LEFT JOIN hatProvince p ON t.provinces=p.provinceID LEFT JOIN hatCity c ON t.city=c.cityID" .
    			" WHERE  (t.userName ='".$userName."' OR t.email='".$userName."') AND t.passwd='".$pwd."'";
    	$resultValue = $dbUtil -> getUserInfo($sql);
    	return $resultValue;
    }
    
    /**
     * 更新后获取用户
     */
    public function queryUsers($userName){
    	$dbUtil = new DB();
    	$sql = "SELECT t.email,t.id,t.userName,t.mind,t.head,p.province as provinces,c.city FROM userInfo t LEFT JOIN hatProvince p ON t.provinces=p.provinceID LEFT JOIN hatCity c ON t.city=c.cityID" .
    			" WHERE  (t.userName ='".$userName."' OR t.email='".$userName."') ";
    	$resultValue = $dbUtil -> getUserInfo($sql);
    	return $resultValue;
    }
    
    /**
     * 修改密码
     */
    public function updatePwd($passwd,$email){
    	$dbUtil = new DB();
    	$sql = "UPDATE userInfo SET passwd='".$passwd."' WHERE email='".$email."'";
    	$resultValue = $dbUtil -> updatePwd($sql);
    	
    	return $resultValue;
    }
}
?>